<?php
class SearchResult extends Model
{	
	public static function search($keyword)
	{		
		$keyword = Database::escape_string($keyword);
		
		$query = "SELECT DISTINCT(SearchResult.nefub_id) as ID, name as Name, type as Type, subtitle as Subtitle, url as URL
					FROM SearchResult
					WHERE SearchResult.name LIKE '%".$keyword."%'
					AND SearchResult.type = 'team'
					ORDER BY  SearchResult.name LIKE '".$keyword."%' DESC, SearchResult.name ASC";
		$result = Database::query($query);
		$teams = Database::convertResult($result);		
		
		$query = "SELECT DISTINCT(SearchResult.nefub_id) as ID, name as Name, type as Type, subtitle as Subtitle, url as URL
					FROM SearchResult
					WHERE SearchResult.name LIKE '%".$keyword."%'
					AND SearchResult.type = 'person'
					ORDER BY  SearchResult.name LIKE '".$keyword."%' DESC, SearchResult.name ASC";
		$result = Database::query($query);
		$persons = Database::convertResult($result);
		
		$results = array_merge($teams, $persons);
		
		return $results;		
	}
	
	/**
	 * 
	 * @param int $nefubId
	 * @param string $type
	 * @return SearchResult
	 */
	public static function selectByNefubId($nefubId, $type)
	{
		return Model::getSingleModel(get_class(), array('nefub_id' => $nefubId, 'type' => $type));
	}
}